package edu.njfu.myapplication1

import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import edu.njfu.homeork.adapter.StudentAdapterByRecyclerView
import edu.njfu.homeork.model.Student
import edu.njfu.homeork.service.StudentService
import edu.njfu.homeork.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_students_info.*
import retrofit2.Call
import retrofit2.Response

class StudentsInfo : AppCompatActivity() {
    var bitmap: Bitmap? =null
    lateinit var students:List<Student>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_students_info)
        var resultByName=intent.getStringExtra("stduentName")
        var type=intent.getStringExtra("type")
        if (type != null) {
            Log.d("asdas",type)
        }
        val serviceCreator=ServerCreator.create<StudentService>()
        serviceCreator.getStudentData().enqueue(object :retrofit2.Callback<List<Student>>{
            override fun onFailure(call: Call<List<Student>>, t: Throwable) {
                TODO("Not yet implemented")
            }

            override fun onResponse(call: Call<List<Student>>, response: Response<List<Student>>) {
                Log.d("null","type==1")

                students = response.body()!!
                if(type==null){
                    var layoutManager=LinearLayoutManager(this@StudentsInfo)
                    recyclerView.layoutManager=layoutManager
                    var adapter= students?.let { StudentAdapterByRecyclerView(it as ArrayList<Student>) }
                    recyclerView.adapter=adapter
                }else{
                    Log.d("null","type==1")
                    var searchList=ArrayList<Student>()
                    students.forEach{
                        if(it.stuName==resultByName){
                            searchList?.add(it)
                            Log.d("null",it.toString())
                        }
                    }
                    if (searchList != null) {
                        if(searchList.size==0){
                            Toast.makeText(this@StudentsInfo,"查询无结果",Toast.LENGTH_LONG).show()
                        }else{
                            val layoutManager=LinearLayoutManager(this@StudentsInfo)
                            recyclerView.layoutManager=layoutManager
                            val adapter= searchList?.let { StudentAdapterByRecyclerView(it) }
                            recyclerView.adapter=adapter
                        }
                    }else{
                        Log.d("null","lsearchL:isg is nul;l")

                    }
                }
            }
        })
        searchBtn.setOnClickListener{
            var name=searchName.text.toString();
            var searchIntent=Intent(this,SearchActivity::class.java)
            searchIntent.putExtra("stduentName",name)
            searchIntent.putExtra("type","1")
            startActivity(searchIntent)
        }

    }
}